package thread;

import com.sun.source.tree.SynchronizedTree;

public class SyncDemo3 {
    public static void main(String[] args) {
        /*静态方法中如果使用同步块,指定的同步监视器对象应当也是类对象
        指定时的语法:类名.class*/
        Boo boo1 =new Boo();
        Boo boo2 =new Boo();
        Thread t1 =new Thread(){
            @Override
            public void run() {
                //Boo.dosome();
                boo1.dosome();
            }
        };
        Thread t2 =new Thread(){
            @Override
            public void run() {
                boo2.dosome();
            }
        };
        t1.start();;
        t2.start();
    }
}
class Boo{
    //public synchronized static void dosome(){//静态方法只有一份,所以对静态方法上锁可以实现同步(也就是排队效果)
        public static void dosome(){
            synchronized (Boo.class) {
                try {
                    Thread t =Thread.currentThread();
                    System.out.println(t.getName()+"正在执行dosome方法");
                    Thread.sleep(5000);
                    System.out.println(t.getName()+"执行dosome执行完毕");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
}